package com.dongdongkeji.base.mvp;

import android.content.Context;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Author: ChenJing
 * Date: 2017-06-20 上午 10:44
 * Version: 1.0
 */

public class BasePresenter<V extends MvpView> implements MvpPresenter<V>{

    protected Context context;
    protected CompositeDisposable disposables;
    private WeakReference<V> viewRef;

    public BasePresenter(Context context, CompositeDisposable disposables) {
        this.context = context;
        this.disposables = disposables;
    }

    @Override
    public void attachView(V view) {
        viewRef = new WeakReference<V>(view);
    }

    @Override
    public void onDestroy() {
        context = null;
        if (disposables != null){
            disposables.dispose();
            disposables.clear();
            disposables = null;
        }
        if (viewRef != null){
            viewRef.clear();
            viewRef = null;
        }
    }

    public V getView(){
        return viewRef == null ? null : viewRef.get();
    }

    public boolean isViewAttached(){
        return viewRef != null && viewRef.get() != null;
    }
}
